using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BrowserManager : MonoBehaviour{
	
	static GameReference reference;
	static Dictionary<string, string> originReturnFunctions;
	
	static GameObject instance;
	
	static Dictionary<string,bool> typeCheckDict;
	
	public static Transform currentNewTransform;
	public static Vector3 CurrentIndex
	{
		get
		{			
			float lowestZ = 0;
			
			for(int i = 0; i < reference.BaseMenuPanel.GetChildCount(); i++)
			{
				Transform t = reference.BaseMenuPanel.GetChild(i);
				if(currentNewTransform != t && t.localPosition.z < lowestZ)
				{
					lowestZ = reference.BaseMenuPanel.GetChild(i).localPosition.z;
				}
			}
			
			return Vector3.back * -lowestZ + Vector3.back * 100;
		}
	}

	static BrowserManager () {
		instance = new GameObject();
		instance.AddComponent<BrowserManager>();
		originReturnFunctions = new Dictionary<string, string>();
		reference = GameObject.FindObjectOfType(typeof(GameReference)) as GameReference;
	}
	
	public static void CreateSkillBrowser(GameObject origin, string OkClickedFunction, List<string> currentSelections)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow skillBrowser = g.GetComponent<BrowseEditWindow>();
		skillBrowser.Setup("Skills", SkillManager.index, currentSelections, HelperFunctions.ObjectType.SKILL);
		skillBrowser.newClickedFunction = "AddNewSkill";
		skillBrowser.infoButtonOverrideFunction = "SkillInfoClick";
		skillBrowser.okClickedFunction = "OkClicked";
		skillBrowser.target = instance;
		skillBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(skillBrowser.name))
		{
			skillBrowser.name = skillBrowser.name + "I";
		}
		
		originReturnFunctions.Add(skillBrowser.name, OkClickedFunction);
	}
	
	void AddNewSkill(BrowseEditWindow returnWindow)
	{
		GameObject g = GameObject.Instantiate(reference.CreateSkillWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		CreateSkillWindow skillWindow = g.GetComponent<CreateSkillWindow>();
		skillWindow.target = returnWindow.gameObject;
		skillWindow.functionName = "ReturnMessageNew";
	}
	
	void SkillInfoClick(BrowseEditWindow returnWindow)
	{
		GameObject g = GameObject.Instantiate(reference.CreateSkillWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		CreateSkillWindow skillWindow = g.GetComponent<CreateSkillWindow>();
		skillWindow.target = returnWindow.gameObject;
		skillWindow.functionName = "RemoveInfoTarget";
		skillWindow.LoadSkillDisplay(returnWindow.infoTarget);
	}
	
	public static void CreateAbilitySubsetBrowser(GameObject origin, string OkClickedFunction, List<string> subset)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		g.transform.localScale = Vector3.one;
		
		BrowseEditWindow abilityBrowser = g.GetComponent<BrowseEditWindow>();
		Dictionary<string,string> abilityDict = new Dictionary<string, string>();
		typeCheckDict = new Dictionary<string, bool>();
		
		foreach(string key in SpecialAbilityManager.index.Keys)
		{
			if(subset.Contains(key))
			{
				abilityDict.Add(key, SpecialAbilityManager.index[key]);
				typeCheckDict.Add(key,false);
			}
		}
		foreach(string key in FeatManager.index.Keys)
		{
			if(subset.Contains(key) || subset.Count == 0)
			{
				abilityDict.Add(key, FeatManager.index[key]);
				typeCheckDict.Add(key,true);
			}
		}
		
		string windowTitle = "Abilities";
		
		abilityBrowser.Setup(windowTitle, abilityDict, new List<string>(), HelperFunctions.ObjectType.ABILITY,  true, true);
		abilityBrowser.infoButtonOverrideFunction = "AbilitySubsetInfoClick";
		abilityBrowser.okClickedFunction = "OkClicked";
		abilityBrowser.target = instance;
		abilityBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(abilityBrowser.name))
		{
			abilityBrowser.name = abilityBrowser.name + "I";
		}
		
		originReturnFunctions.Add(abilityBrowser.name, OkClickedFunction);
		
		abilityBrowser.NewButton.SetActiveRecursively(false);
	}
	
	void AbilitySubsetInfoClick(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewAbilityWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		CreateAbilityWindow skillWindow = g.GetComponent<CreateAbilityWindow>();
		skillWindow.target = edit.gameObject;
		skillWindow.LoadAbilityDisplay(edit.infoTarget,typeCheckDict[edit.infoTarget], true);
	}
	
	public static void CreateAbilityBrowser(GameObject origin, string OkClickedFunction, List<string> currentSelections, bool includeFeats, bool includeSpecials, bool DisableNewAndInfo = false)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		if(DisableNewAndInfo)
		{
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
		}
		else
		{
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
		}
		
		g.transform.localScale = Vector3.one;
		
		
		BrowseEditWindow abilityBrowser = g.GetComponent<BrowseEditWindow>();
		Dictionary<string,string> abilityDict = new Dictionary<string, string>();
		typeCheckDict = new Dictionary<string, bool>();
		if(includeSpecials)
		{
			foreach(string key in SpecialAbilityManager.index.Keys)
			{
				abilityDict.Add(key, SpecialAbilityManager.index[key]);
				typeCheckDict.Add(key,false);
			}
		}
		if(includeFeats)
		{
			foreach(string key in FeatManager.index.Keys)
			{
				abilityDict.Add(key, FeatManager.index[key]);
				typeCheckDict.Add(key,true);
			}
		}
		
		string windowTitle = ((includeFeats)?"Feats":"") + ((includeFeats&&includeSpecials)?"/":"") + ((includeSpecials)?"Sp.Ab.":"");
		
		abilityBrowser.Setup(windowTitle, abilityDict, currentSelections, HelperFunctions.ObjectType.ABILITY,  DisableNewAndInfo);
		abilityBrowser.newClickedFunction = "AddNewAbility";
		abilityBrowser.infoButtonOverrideFunction = "AbilityInfoClick";
		abilityBrowser.okClickedFunction = "OkClicked";
		abilityBrowser.target = instance;
		abilityBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(abilityBrowser.name))
		{
			abilityBrowser.name = abilityBrowser.name + "I";
		}
		
		originReturnFunctions.Add(abilityBrowser.name, OkClickedFunction);
		
		if(DisableNewAndInfo)
		{
			abilityBrowser.NewButton.SetActiveRecursively(false);
		}
	}
	
	IEnumerator AddNewAbility(BrowseEditWindow edit)
	{
		if(edit.Title.text.IndexOf("/") >= 0)
		{
			yield return StartCoroutine(AddNewAbilityGeneric(edit));
		}
		else if(edit.Title.text == "Browse Feats")
		{
			AddNewFeat(edit);
		}
		else
		{
			AddNewSpecialAbility(edit);
		}
	}
	
	IEnumerator AddNewAbilityGeneric(BrowseEditWindow edit)
	{
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("", "Feat", false, new List<string>{"Feat","Special Ability"})
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("New Feat or Sp. Ability?", descVar));
		
		if(descVar[0].outVariable == "Feat")
		{
			AddNewFeat(edit);
		}
		else
		{
			AddNewSpecialAbility(edit);
		}
	}
	
	void AddNewFeat(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewAbilityWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		CreateAbilityWindow w = g.GetComponent<CreateAbilityWindow>();
		w.isFeat = true;
		w.target = edit.gameObject;
		w.functionName = "ReturnMessageNew";
	}
	
	void AddNewSpecialAbility(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewAbilityWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		CreateAbilityWindow w = g.GetComponent<CreateAbilityWindow>();
		w.isFeat = false;
		w.target = edit.gameObject;
		w.functionName = "ReturnMessageNew";
	}
	
	void AbilityInfoClick(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewAbilityWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		CreateAbilityWindow skillWindow = g.GetComponent<CreateAbilityWindow>();
		skillWindow.target = edit.gameObject;
		skillWindow.functionName = "RemoveInfoTarget";
		skillWindow.LoadAbilityDisplay(edit.infoTarget,typeCheckDict[edit.infoTarget]);
	}
	
	void OkClicked(BrowseEditWindow edit)
	{
		edit.origin.SendMessage(originReturnFunctions[edit.name], edit.toggledEntries, SendMessageOptions.DontRequireReceiver);
	}
	
	public static void CreateCharacterBrowser(GameObject origin, string OkClickedFunction)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow characterBrowser = g.GetComponent<BrowseEditWindow>();
		characterBrowser.Setup("Characters", SheetManager.index, new List<string>(), HelperFunctions.ObjectType.SHEET, false, true);
		characterBrowser.infoButtonOverrideFunction = "CharacterInfoClick";
		characterBrowser.okClickedFunction = "OkClicked";
		characterBrowser.target = instance;
		characterBrowser.origin = origin;
		characterBrowser.NewButton.SetActiveRecursively(false);
		
		while(originReturnFunctions.ContainsKey(characterBrowser.name))
		{
			characterBrowser.name = characterBrowser.name + "I";
		}
		
		originReturnFunctions.Add(characterBrowser.name, OkClickedFunction);
	}
	
	IEnumerator CharacterInfoClick(BrowseEditWindow edit)
	{
		List<SetterWindowDescription> options = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("","",true,
				new List<string>{"View Description", "Delete Character"},
				false)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Character Options", options));
		
		if(options[0].outVariable != null)
		{
			if(options[0].outVariable == "View Description")
			{
				CharacterSheet character = SheetManager.LoadCharacter(edit.infoTarget);
				if(character != null)
				{
					yield return StartCoroutine(HelperFunctions.ConfirmationWindow(character.description, null));
				}
			}
			else
			{
				List<bool> outputs = new List<bool>{false};
				yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to delete this character? Deletion is permanent.", outputs));
				
				if(outputs[0])
				{
					SheetManager.DeleteCharacter(edit.infoTarget);
				}
			}
		}
		
		yield break;
	}
	
	public static void CreateClassBrowser(GameObject origin, string OkClickedFunction, List<string> currentSelections)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow classBrowser = g.GetComponent<BrowseEditWindow>();
		classBrowser.Setup("Classes", ClassManager.index, currentSelections, HelperFunctions.ObjectType.CLASS, false, true);
		classBrowser.newClickedFunction = "AddNewClass";
		classBrowser.infoButtonOverrideFunction = "ClassInfoClick";
		classBrowser.okClickedFunction = "OkClicked";
		classBrowser.target = instance;
		classBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(classBrowser.name))
		{
			classBrowser.name = classBrowser.name + "I";
		}
		
		originReturnFunctions.Add(classBrowser.name, OkClickedFunction);
	}
	
	void AddNewClass(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewClassWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		ClassEditorMenu w = g.GetComponent<ClassEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "ReturnMessageNew";
	}
	
	void ClassInfoClick(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewClassWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		ClassEditorMenu w = g.GetComponent<ClassEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "RemoveInfoTarget";
		w.LoadClassDisplay(edit.infoTarget);
	}
	
	public static void CreateRaceBrowser(GameObject origin, string OkClickedFunction, List<string> currentSelections)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow raceBrowser = g.GetComponent<BrowseEditWindow>();
		raceBrowser.Setup("Races", RaceManager.index, currentSelections, HelperFunctions.ObjectType.RACE, false, true);
		raceBrowser.newClickedFunction = "AddNewRace";
		raceBrowser.infoButtonOverrideFunction = "RaceInfoClick";
		raceBrowser.okClickedFunction = "OkClicked";
		raceBrowser.target = instance;
		raceBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(raceBrowser.name))
		{
			raceBrowser.name = raceBrowser.name + "I";
		}
		
		originReturnFunctions.Add(raceBrowser.name, OkClickedFunction);
	}
	
	void AddNewRace(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewRaceWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		RaceEditorMenu w = g.GetComponent<RaceEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "ReturnMessageNew";
	}
	
	void RaceInfoClick(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewRaceWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		RaceEditorMenu w = g.GetComponent<RaceEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "RemoveInfoTarget";
		w.LoadRaceDisplay(edit.infoTarget);
	}
	
	public static void CreateSpellBrowser_SubIndex(GameObject origin, string OkClickedFunction, List<string> currentSelections, string spellList, int spellLevel)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow spellBrowser = g.GetComponent<BrowseEditWindow>();
		
		Dictionary<string, string> spellDict = new Dictionary<string, string>();
		foreach(var s in SpellManager.index)
		{
			if(SpellManager.spellsByListAndLevel[s.Key].Contains(spellList+"_"+spellLevel+"_"))
				spellDict.Add(s.Key, s.Value);
		}
		
		spellBrowser.Setup("Spells", spellDict, currentSelections, HelperFunctions.ObjectType.SPELL, false, false);
		spellBrowser.newClickedFunction = "AddNewSpell";
		spellBrowser.infoButtonOverrideFunction = "SpellInfoClick";
		spellBrowser.okClickedFunction = "OkClicked";
		spellBrowser.target = instance;
		spellBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(spellBrowser.name))
		{
			spellBrowser.name = spellBrowser.name + "I";
		}
		
		originReturnFunctions.Add(spellBrowser.name, OkClickedFunction);
	}
	
	public static void CreateSpellBrowser(GameObject origin, string OkClickedFunction, List<string> currentSelections)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow spellBrowser = g.GetComponent<BrowseEditWindow>();
		spellBrowser.Setup("Spells", SpellManager.index, currentSelections, HelperFunctions.ObjectType.SPELL, false, false);
		spellBrowser.newClickedFunction = "AddNewSpell";
		spellBrowser.infoButtonOverrideFunction = "SpellInfoClick";
		spellBrowser.okClickedFunction = "OkClicked";
		spellBrowser.target = instance;
		spellBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(spellBrowser.name))
		{
			spellBrowser.name = spellBrowser.name + "I";
		}
		
		originReturnFunctions.Add(spellBrowser.name, OkClickedFunction);
	}
	
	void AddNewSpell(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewSpellWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		SpellEditorMenu w = g.GetComponent<SpellEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "ReturnMessageNew";
	}
	
	void SpellInfoClick(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewSpellWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		SpellEditorMenu w = g.GetComponent<SpellEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "RemoveInfoTarget";
		w.LoadSpellDisplay(edit.infoTarget);
	}
	
	public static void CreateBuffsBrowser(GameObject origin, string OkClickedFunction, List<string> currentSelections, bool singleSelect = true)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow buffsBrowser = g.GetComponent<BrowseEditWindow>();
		buffsBrowser.Setup("Buff/Debuffs", BuffDebuffManager.index, currentSelections, HelperFunctions.ObjectType.BUFF, false, singleSelect);
		buffsBrowser.newClickedFunction = "AddNewBuff";
		buffsBrowser.infoButtonOverrideFunction = "BuffInfoClick";
		buffsBrowser.okClickedFunction = "OkClicked";
		buffsBrowser.target = instance;
		buffsBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(buffsBrowser.name))
		{
			buffsBrowser.name = buffsBrowser.name + "I";
		}
		
		originReturnFunctions.Add(buffsBrowser.name, OkClickedFunction);
	}
	
	void AddNewBuff(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewBuffWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BuffEditorMenu w = g.GetComponent<BuffEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "ReturnMessageNew";
	}
	
	void BuffInfoClick(BrowseEditWindow edit)
	{
		GameObject g = Instantiate(reference.NewBuffWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BuffEditorMenu w = g.GetComponent<BuffEditorMenu>();
		w.target = edit.gameObject;
		w.functionName = "RemoveInfoTarget";
		w.LoadBuffDisplay(edit.infoTarget);
	}
	
	public static void CreateItemsBrowser(GameObject origin, string OkClickedFunction, List<string> currentSelections, bool WeaponsOnly = false)
	{
		GameObject g = GameObject.Instantiate(reference.BrowseSelectSearchWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		currentNewTransform = g.transform;
		g.transform.localPosition = CurrentIndex;
		
		BrowseEditWindow itemsBrowser = g.GetComponent<BrowseEditWindow>();
		Dictionary<string, string> itemsIndex = new Dictionary<string, string>();
		
		if(!WeaponsOnly)
		{
			foreach(string key in ItemManager.index.Keys)
			{
				itemsIndex.Add(key + "$", ItemManager.index[key]);
			}
		}
		foreach(string key in WeaponManager.index.Keys)
		{
			itemsIndex.Add(key + "^", WeaponManager.index[key]);
		}
		/*foreach(string key in ArmorManager.index.Keys)
		{
			itemsIndex.Add(key + "@", ArmorManager.index[key]);
		}*/
		
		itemsBrowser.Setup("Equipment", itemsIndex, currentSelections, HelperFunctions.ObjectType.ITEM, false, false);
		itemsBrowser.newClickedFunction = "AddNewItem";
		itemsBrowser.infoButtonOverrideFunction = "ItemInfoClick";
		itemsBrowser.okClickedFunction = "OkClicked";
		itemsBrowser.target = instance;
		itemsBrowser.origin = origin;
		
		while(originReturnFunctions.ContainsKey(itemsBrowser.name))
		{
			itemsBrowser.name = itemsBrowser.name + "I";
		}
		
		originReturnFunctions.Add(itemsBrowser.name, OkClickedFunction);
	}
	
	IEnumerator AddNewItem(BrowseEditWindow edit)
	{
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("", "Item", false, new List<string>{"Equipment","Weapon"})
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Item Type?", descVar));
		
		if(descVar[0].outVariable == "Equipment")
		{
			GameObject g = Instantiate(reference.NewItemWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
			
			ItemEditorMenu w = g.GetComponent<ItemEditorMenu>();
			w.target = edit.gameObject;
			w.functionName = "ReturnMessageNew";
		}
		else if(descVar[0].outVariable == "Weapon")
		{
			GameObject g = Instantiate(reference.NewWeaponWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
			
			WeaponEditorMenu w = g.GetComponent<WeaponEditorMenu>();
			w.target = edit.gameObject;
			w.functionName = "ReturnMessageNew";
		}
		else if(descVar[0].outVariable != null)
		{
			GameObject g = Instantiate(reference.NewArmorWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
			
			BuffEditorMenu w = g.GetComponent<BuffEditorMenu>();
			w.target = edit.gameObject;
			w.functionName = "ReturnMessageNew";	
		}
	}
	
	void ItemInfoClick(BrowseEditWindow edit)
	{	
		char marker = edit.infoTarget[edit.infoTarget.Length-1];
		string itemName = edit.infoTarget.Remove(edit.infoTarget.Length-1);
		
		Debug.Log(edit.infoTarget);
		
		if(marker == '@')
		{
			GameObject g = Instantiate(reference.NewItemWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
			BuffEditorMenu w = g.GetComponent<BuffEditorMenu>();
			w.target = edit.gameObject;
			w.functionName = "RemoveInfoTarget";
			w.LoadBuffDisplay(itemName);
		}
		else if(marker == '^')
		{
			GameObject g = Instantiate(reference.NewWeaponWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
			WeaponEditorMenu w = g.GetComponent<WeaponEditorMenu>();
			w.target = edit.gameObject;
			w.functionName = "RemoveInfoTarget";
			w.LoadItemDisplay(itemName);
		}
		else
		{
			GameObject g = Instantiate(reference.NewItemWindowPrefab) as GameObject;
			g.transform.parent = GameReference.reference.BaseMenuPanel;
			g.transform.localScale = Vector3.one;
			currentNewTransform = g.transform;
			g.transform.localPosition = CurrentIndex;
			ItemEditorMenu w = g.GetComponent<ItemEditorMenu>();
			w.target = edit.gameObject;
			w.functionName = "RemoveInfoTarget";
			w.LoadItemDisplay(itemName);
		}
	}
}
